using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Rendering;
using System;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace IQIGame.Onigao.GamePlay
{
    public abstract class TSceneController<TSceneLoader> : TModuleController where TSceneLoader : SceneLoaderProcessDefault, new()
    {
        private const string SceneExtraRootName = "SceneExtra";
        protected TSceneLoader _loaderProcess;
        private GameObject _envGo;

        public GameObject sceneExtraRoot { get; private set; }
        protected Collider groundCollider { get; private set; }
        public ProceduralSkybox skybox { get; protected set; }

        public TSceneController()
        {
            _loaderProcess = new TSceneLoader();
        }

        /// <summary>
        /// 添加需要被loadingUI覆盖的预加载
        /// 注意该预加载的实际执行时间在场景文件加载完成以后，loadingUI关闭之前
        /// </summary>
        /// <param name="preloadFunc"></param>
        public void AddPreloadAfterScene(Func<UniTask> preloadFunc)
        {
            _loaderProcess.AddAsyncPreload(preloadFunc);
        }

        public void AddFirstPreloadAfterScene(Func<UniTask> preloadFunc)
        {
            _loaderProcess.AddAsyncPreloadFirst(preloadFunc);
        }

        protected async UniTask LoadScene(string sceneName, LoadSceneMode loadMode = LoadSceneMode.Single)
        {
            await ManagerCenter.Scene.LoadAbSceneAsync(sceneName, loadMode, _loaderProcess);
            if (!GameObject.Find(SceneExtraRootName))
            {
                sceneExtraRoot = new GameObject(SceneExtraRootName);
            }
            _envGo = GameObject.Find("Env");
            if (_envGo == null)
            {
                Debug.LogError("场景上没有Env节点。");
            }
            else
            {
                var colliders = _envGo?.GetComponentsInChildren<Collider>();
                foreach (var collider in colliders)
                {
                    if (collider.gameObject.layer == (int)LayerConsts.Ground)
                    {
                        groundCollider = collider;
                    }
                }
            }
            if (groundCollider == null)
            {
                LogGame.LogError("场景中未找到layer == Ground的Collider，角色可能会浮空，请确保在Env节点下有已经激活的，layer为Ground的地面碰撞器。");
            }
            var globalVolumeObj = GameObject.Find("Env/Global Volume");
            if (globalVolumeObj == null)
            {
                LogGame.LogError("场景中未找到Env/Global Volume，对全局显示效果的控制将失效，请确认路径是否符合规范。");
            }
            skybox = _envGo.GetComponentInChildren<ProceduralSkybox>();
        }

        public void SetBaseObjActive(bool isActive)
        {
            if (groundCollider != null)
            {
                groundCollider.enabled = isActive;
            }
            _envGo?.SetActive(isActive);
        }

        public override void Dispose()
        {
            base.Dispose();
            groundCollider = null;
            _envGo = null;
        }
    }
}
